package com.docios.cookbook2.utils;

import com.google.android.material.appbar.AppBarLayout;

public abstract class LayoutListener implements AppBarLayout.OnOffsetChangedListener{

    public enum State{EXPANDED, COLLAPSED}
    private State mCurrentState = State.EXPANDED;

    public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        // verticalOffset 为垂直偏移量，当偏移量等于0时，表示完全展开
        // appBarLayout.getTotalScrollRange() 返回滚动范围，一般是CollapsingToolbarLayout的总高度
        if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
            // CollapsingToolbarLayout 完全折叠
            if (mCurrentState!=State.COLLAPSED){
                onStateChanged(appBarLayout,State.COLLAPSED);
            }
            mCurrentState=State.COLLAPSED;
        } else {// CollapsingToolbarLayout 未完全折叠
            if (mCurrentState!=State.EXPANDED){
                onStateChanged(appBarLayout,State.EXPANDED);
            }
            mCurrentState=State.EXPANDED;
        }
    }
}
